// 数据库连接配置
const mysql = require('mysql2/promise')
let conf
if (process.env.NODE_ENV === 'production') {
  // 正式数据库
  conf = {
    host: '139.224.80.73',
    user: 'prophet',
    password: 'wallet',
    database: 'wallet',
    charset: 'utf8mb4',
    port: '3306',
    connectionLimit: 50
  }
} else {
  conf = {
    host: '139.224.80.73',
    user: 'prophet',
    password: 'wallet',
    database: 'wallet_test',
    charset: 'utf8mb4',
    port: '3306',
    connectionLimit: 50
  }
}

class Db {
  constructor () {
    this.pool = mysql.createPool(conf)
      // console.log(mysql, this.pool)
  }
    // 查询函数
  async querySql (sql, options) {
    try {
      let results = await this.pool.query(sql, options)

      console.log('query sql result:', results[0])

      if (results[0]) return results[0]
      else return
    } catch (err) {
      throw new Error(err)
    }
  }
}

module.exports = new Db()
